//
//  MallLogisticsViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/9/16.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "MallLogisticsViewController.h"
#import "OrderExpress.h"
#import "Express.h"
#import "ExpressTraces.h"

#import "ExpressCell.h"

@interface MallLogisticsViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) OrderExpress *orderExpressModel;

@property (nonatomic, strong) NSMutableArray *tracesArr;

@end

@implementation MallLogisticsViewController {
    
    UIView          *_headBgView;
    UIImageView     *_shopPic;
    UILabel         *_stateL;
    UILabel         *_companyL;
    UILabel         *_numberL;
    UILabel         *_phoneL;
    UIView          *_lineV;
    UILabel         *_tableHeadL;
    
    UITableView     *_tableView;
    ExpressCell     *_cell;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createUI];
    [self GETLogistics];
    
    
}


#pragma mark ==========构建UI==========
- (void)createUI {
    
    self.navigationItem.title = @"物流查询";
    
    self.view.backgroundColor = UISeparatedColorRGB;
    
    _headBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20+SW(4))];
    _headBgView.backgroundColor = kWhiteColor;
    [self.view addSubview:_headBgView];
    
    _shopPic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SW(4), SW(4))];
    [_headBgView addSubview:_shopPic];
    
    CGFloat labelX = _shopPic.x+_shopPic.width+10;
    CGFloat labelW = SCREEN_WIDTH-labelX;
    CGFloat labelH = _shopPic.height/4;
    
    _stateL = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 10, labelW, labelH)];
    [_headBgView addSubview:_stateL];
    _stateL.font = kUHSystemFontWithSize(12);
    _stateL.textColor = kBlackColor;
    
    _companyL = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 10+labelH, labelW, labelH)];
    [_headBgView addSubview:_companyL];
    _companyL.font = kUHSystemFontWithSize(10);
    _companyL.textColor = kGrayColor;
    
    _numberL = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 10+labelH*2, labelW, labelH)];
    [_headBgView addSubview:_numberL];
    _numberL.font = kUHSystemFontWithSize(10);
    _numberL.textColor = kGrayColor;
    
    _phoneL = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 10+labelH*3, labelW, labelH)];
    [_headBgView addSubview:_phoneL];
    _phoneL.font = kUHSystemFontWithSize(10);
    _phoneL.textColor = kGrayColor;
    
//    _lineV = [[UIView alloc] initWithFrame:CGRectMake(0, _headBgView.height, SCREEN_WIDTH, 10)];
//    [self.view addSubview:_lineV];
    
    _tableHeadL = [[UILabel alloc] initWithFrame:CGRectMake(0, _headBgView.height+10, SCREEN_WIDTH, 40)];
    [self.view addSubview:_tableHeadL];
    _tableHeadL.font = kUHSystemFontWithSize(14);
    _tableHeadL.text = @"  物流跟踪";
    _tableHeadL.textColor = kGrayColor;
    _tableHeadL.backgroundColor = kWhiteColor;
    
    
    CGFloat tableY = _tableHeadL.y+_tableHeadL.height+1;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, tableY, SCREEN_WIDTH, SCREEN_HEIGHT-tableY-64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
//    _tableView.userInteractionEnabled = NO;
    
    [_tableView registerNib:[UINib nibWithNibName:@"ExpressCell" bundle:nil] forCellReuseIdentifier:@"express"];
    
}

#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    _cell = [tableView dequeueReusableCellWithIdentifier:@"express"];
    
    NSLog(@"%@", self.tracesArr[indexPath.row]);
    
    ExpressTraces *model = [[ExpressTraces alloc] init];
    [model setValuesForKeysWithDictionary:self.tracesArr[indexPath.row]];
    
    _cell.row = indexPath.row;
    _cell.model = model;
//    _cell.state.text = @"x";
//    _cell.time.text = @"y";
    
    return _cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tracesArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _cell.time.y+_cell.time.height+10;
//    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
}
#pragma mark ============响应方法============
- (void)GETLogistics {
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/expresses", APIMallsOrders, self.goodId];
    
    [[NetworkTool sharedTool] requestWithName:@"查询物流" URLString:url parameters:nil method:@"GET" callBack:^(id responseObject) {
        
        
        self.orderExpressModel = [[OrderExpress alloc] init];
        [self.orderExpressModel setValuesForKeysWithDictionary:responseObject[@"data"]];
        
        Express *express = [[Express alloc] init];
        [express setValuesForKeysWithDictionary:[self.orderExpressModel valueForKey:@"express"]];
        
        // 物流跟踪信息
        [self.tracesArr addObjectsFromArray:[[express.Traces reverseObjectEnumerator] allObjects]];
        
        [_tableView reloadData];
        
        // 头像
        NSString *picStr = [[self.orderExpressModel.order valueForKey:@"shop"] valueForKey:@"photo"];
        [_shopPic sd_setImageWithURL:[NSURL URLWithString:picStr] placeholderImage:IMAGE(@"placeHolder")];
        
        //
        NSDictionary *dic = @{@"0":@"无轨迹", @"1":@"已揽件", @"2":@"在途中", @"201":@"到达派件城市", @"3":@"签收", @"4":@"问题件"};
        _stateL.text = [NSString stringWithFormat:@"物流状态:%@", dic[[NSString stringWithFormat:@"%ld", express.State]]];

        _companyL.text = [NSString stringWithFormat:@"物流公司:%@", express.ShipperName];
        
        _numberL.text = [NSString stringWithFormat:@"快递单号:%@", [self.orderExpressModel.order valueForKey:@"express_no"]];
        
//        _phoneL.text = [NSString stringWithFormat:@"%@", ] 没有
        _phoneL.text = @"官方电话:暂无";
        
    }];
    
}

// 懒加载
- (NSMutableArray *)tracesArr {
    if (_tracesArr == nil) {
        _tracesArr = [[NSMutableArray alloc] init];
    }
    return _tracesArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
